﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dingding.Helper
{
    public class StackExchange_Redis
    { 
        private static string connstr = "192.168.121.248:6380,password=zhang456123"; //no
         
        /// <summary>
        /// 获取bool的Redis
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        public static Boolean GetRedis_Bool(string myKey)
        {
            Boolean msg = false;
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(connstr);
            try
            {  
                // 获取数据库
                IDatabase db = redis.GetDatabase(1); 
                // 读取数据
                string value = db.StringGet(myKey);

                if (value ==null)
                {
                    msg = false;
                }
                else if (value == "true")
                {
                    msg = true; 
                } 
            }
            catch (Exception ex)
            { 
                throw new Exception(ex.Message);
            }
            finally
            {
                redis.Close();
            }
            return msg;
            
        }

        public static string GetRedis_String(string myKey)
        {
            string  msg = "";
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(connstr);
            try
            {
                // 获取数据库
                IDatabase db = redis.GetDatabase(1);
                // 读取数据
                msg = db.StringGet(myKey);  
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                redis.Close();
            }
            return msg;

        }



    }
}